<?php
require_once 'libraries/utilites/functions.php';

$path = "../ptnhuan-web1/data/films.txt";
$full_path = realpath($path);
$films = mreadFile($path);
$rating = NULL;
$rating1 = NULL;
?>

<form method="POST">
    From : <input type="text" name="rating">.<br>
    To :   <input type="text" name="rating1">.<br>
    <input type="submit" name="submit" value="Search">
</form>

<?php if (isset($_POST['submit'])): ?>
    <?php $rating = (double) $_POST['rating']; ?>
    <?php $rating1 = (double) $_POST['rating1']; ?>
<?php endif; ?>
<?php foreach ($films as $item): ?>
    <?php
    $item = trim($item);
    if (!empty($item)):
        ?>
        <?php
        $film = explode(";", $item);
        $img = trim($film[1]);
        ?>
        <?php if (empty($rating) && empty($rating1)): ?>
            <?php if (isset($film[0]) || isset($film[2]) || isset($film[3])): ?>
                <?php echo $film[0] ?>
                <a href="edit.php?if=<?php echo $film[0] ?>">Edit</a>
                <img src = '../ptnhuan-web1/public/images/films/<?php echo $img ?>'> 
                <?php echo $film[2] ?>
                <?php echo $film[3] ?>

                <br>
            <?php endif; ?>
        <?php else: ?>
            <?php if (isset($film[0]) || isset($film[2]) || isset($film[3])): ?>
                <?php if (($rating <= (double) $film[3]) && ($rating1 >= (double) $film[2])): ?>
                    <?php echo $film[0] ?>
                     <a href="edit.php?if=<?php echo $film[0] ?>">Edit</a>
                    <img src = '../ptnhuan-web1/public/images/films/<?php echo $img ?>'> 
                    <?php echo $film[2] ?>
                    <?php if (!empty($film[3])) echo $film[3] ?>                

                    <br>
                <?php endif; ?>
            <?php endif; ?>
        <?php endif; ?> 
    <?php endif; ?>             
<?php endforeach; ?>   